package visao;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.MaskFormatter;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.UIManager;

import java.awt.Component;

import javax.swing.Box;
import javax.swing.JProgressBar;
import javax.swing.JSeparator;
import javax.swing.JMenuItem;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import modelo.Evento;
import modelo.Estadio;
import modelo.Funcionario;
import modelo.Time;
import controle.ControleEvento;
import controle.ControleEstadio;
import controle.ControleTime;
import de.javasoft.plaf.synthetica.SyntheticaBlackEyeLookAndFeel;
import de.javasoft.plaf.synthetica.SyntheticaSimple2DLookAndFeel;

import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.sql.Date;
/**
 * 
 * @author douglas
 */
public class CadastroEvento extends JFrame {

	Evento evento = new Evento();
	Estadio estadio = new Estadio();
	Time time = new Time();

	private ControleEstadio controleEstadio;
	private ControleEvento controleEvento;
	private ControleTime controleTime;

	private SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

	private JPanel contentPane;
	private JTextField txtData;
	private JTextField txtHora;
	private JTextField txtPrecoCamarote;
	private JTextField txtPrecoGeral;
	private JComboBox cbbEvento;
	private JComboBox cbbEstadio;
	private JComboBox cbbTime1;
	private JComboBox cbbTime2;
	private Funcionario funcionario;
	

	
        /**
         * Metodo para abrir tela
         * 
         * @param status
         * @param funcionario 
         */
	protected void abreTela(int status, Funcionario funcionario) {
		this.funcionario = funcionario;
		for (int i = 0; i < controleTime.buscarTodos().size(); i++) {
			cbbTime2.addItem(controleTime.buscarTodos().get(i));
		}
		for (int i = 0; i < controleTime.buscarTodos().size(); i++) {
			cbbTime1.addItem(controleTime.buscarTodos().get(i));
		}
		for (int i = 0; i < controleEstadio.buscarTodos().size(); i++) {
			cbbEstadio.addItem(controleEstadio.buscarTodos().get(i));
		}
		for (int i = 0; i < controleEvento.buscarTodos().size(); i++) {
			cbbEvento.addItem(controleEvento.buscarTodos().get(i));
		}
		for (Time m : controleTime.buscarTodos()) {
			cbbTime1.addItem(m);
		}
		for (Time m : controleTime.buscarTodos()) {
			cbbTime2.addItem(m);
		}

	}
        /**
         * 
         * @param args metodo main
         */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UIManager.setLookAndFeel(new SyntheticaSimple2DLookAndFeel());
					CadastroEvento frame = new CadastroEvento();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Cria@@@@o do frame.
	 */
	public CadastroEvento() {
		controleEvento = new ControleEvento();
		controleEstadio = new ControleEstadio();
		controleTime = new ControleTime();

		setTitle("Cadastro de Evento");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 511, 452);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblEstadio = new JLabel("Estadio");
		lblEstadio.setBounds(10, 120, 46, 14);
		contentPane.add(lblEstadio);

		/**
		 * combobox de estadios
		 */
		cbbEstadio = new JComboBox();
		cbbEstadio.setBounds(10, 134, 283, 22);
		contentPane.add(cbbEstadio);

		/**
		 * combobox de eventos
		 */
		cbbEvento = new JComboBox();
		cbbEvento.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
			}
		});
		cbbEvento.setBounds(10, 51, 294, 23);
		contentPane.add(cbbEvento);

		/**
		 * combobox de Time1
		 */
		cbbTime1 = new JComboBox();
		cbbTime1.setBounds(305, 135, 168, 20);
		contentPane.add(cbbTime1);

		/**
		 * combobox de Time2
		 */
		cbbTime2 = new JComboBox();
		cbbTime2.setBounds(314, 192, 159, 20);
		contentPane.add(cbbTime2);

		JLabel lblData = new JLabel("Data");
		lblData.setBounds(10, 178, 46, 14);
		contentPane.add(lblData);

		JLabel lblTime = new JLabel("Time 1");
		lblTime.setBounds(324, 120, 46, 14);
		contentPane.add(lblTime);

		JLabel lblTime_2 = new JLabel("Time 2");
		lblTime_2.setBounds(324, 178, 46, 14);
		contentPane.add(lblTime_2);

		JLabel lblPreoCamarote = new JLabel("Pre\u00E7o Camarote");
		lblPreoCamarote.setBounds(10, 284, 154, 14);
		contentPane.add(lblPreoCamarote);

		JLabel lblPreoGeral = new JLabel("Pre\u00E7o Geral");
		lblPreoGeral.setBounds(10, 334, 83, 14);
		contentPane.add(lblPreoGeral);

		JLabel lblHora = new JLabel("Hora");
		lblHora.setBounds(10, 222, 46, 14);
		contentPane.add(lblHora);

		txtData = new JTextField();

		try {
			txtData = new JFormattedTextField(new MaskFormatter("##/##/####"));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		txtData.setBounds(10, 191, 204, 20);
		contentPane.add(txtData);
		txtData.setColumns(10);

		try {
			txtHora = new JFormattedTextField(new MaskFormatter("##:##"));
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		txtHora.setBounds(10, 235, 204, 20);
		contentPane.add(txtHora);
		txtHora.setColumns(10);

		txtPrecoCamarote = new JTextField();
		txtPrecoCamarote.setBounds(30, 298, 86, 20);
		contentPane.add(txtPrecoCamarote);
		txtPrecoCamarote.setColumns(10);

		txtPrecoGeral = new JTextField();
		txtPrecoGeral.setBounds(30, 346, 86, 20);
		contentPane.add(txtPrecoGeral);
		txtPrecoGeral.setColumns(10);

		JLabel lblR = new JLabel("R$");
		lblR.setBounds(10, 301, 46, 14);
		contentPane.add(lblR);

		JLabel lblR_1 = new JLabel("R$");
		lblR_1.setBounds(10, 349, 46, 14);
		contentPane.add(lblR_1);
                /**
                 * Bot@@o Buscar
                 */
		JButton btnBuscar = new JButton("Buscar");
		btnBuscar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (cbbEvento.getItemCount() > 0) {
					evento = (Evento) cbbEvento.getSelectedItem();

					txtData.setText(sdf.format(evento.getData()));
					txtHora.setText(String.valueOf(evento.getHoras()));
					txtPrecoCamarote.setText(String.valueOf(evento
							.getPreco_camarote()));
					txtPrecoGeral.setText(String.valueOf(evento
							.getPreco_geral()));

					cbbTime1.setSelectedItem(evento.getTime_1());
					cbbTime2.setSelectedItem(evento.getTime_2());

					cbbEstadio.setSelectedItem(evento.getEstadio());

				} else {
					JOptionPane.showMessageDialog(null,
							"Nenhum Evento para mostrar");
				}
			}
		});
		btnBuscar.setBounds(315, 51, 91, 23);
		contentPane.add(btnBuscar);
                /**
                 * Bot@@o Cadastrar
                 */
		JButton btnCadastrar = new JButton("Cadastrar");
		btnCadastrar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				estadio = (Estadio) cbbEstadio.getSelectedItem();
				time = (Time) cbbTime1.getSelectedItem();
				time = (Time) cbbTime2.getSelectedItem();

				evento.setEstadio(estadio);
				evento.setTime_1(time);
				evento.setTime_2(time);
				SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
				try {
					evento.setData(sdf.parse(txtData.getText()));
				} catch (ParseException e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				}

				SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm");

				try {
					evento.setHoras(sdf2.parse(txtHora.getText()));
				} catch (ParseException e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				}
				evento.setPreco_camarote(Double.valueOf(txtPrecoCamarote
						.getText()));
				evento.setPreco_geral(Double.valueOf(txtPrecoGeral.getText()));

				try {

					controleEvento.salvar(evento);
					JOptionPane.showMessageDialog(null, "Evento Salvo");
					controleEstadio.excluir(estadio);

				} catch (Exception e1) {
					JOptionPane.showMessageDialog(null,
							"erro" + e1.getMessage());
					e1.printStackTrace();
				}

				Menu menu = new Menu();
				menu.abreTela(1, funcionario);
				menu.setVisible(true);
				dispose();

			}
		});
		btnCadastrar.setBounds(56, 391, 91, 23);
		contentPane.add(btnCadastrar);
                /**
                 * Bot@@o Editar
                 */
		JButton btnEditar = new JButton("Editar");
		btnEditar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				estadio = (Estadio) cbbEstadio.getSelectedItem();
				time = (Time) cbbTime1.getSelectedItem();
				time = (Time) cbbTime2.getSelectedItem();

				evento.setEstadio(estadio);
				evento.setTime_1(time);
				evento.setTime_2(time);
				evento.setData(Date.valueOf(txtData.getText()));

				evento.setHoras(java.sql.Time.valueOf(txtHora.getText()));
				evento.setPreco_camarote(Double.valueOf(txtPrecoCamarote
						.getText()));
				evento.setPreco_geral(Double.valueOf(txtPrecoGeral.getText()));

				try {

					controleEvento.salvar(evento);

				} catch (Exception e1) {
					JOptionPane.showMessageDialog(null,
							"erro" + e1.getMessage());
					// e1.printStackTrace();
				}

			}
		});
		btnEditar.setBounds(219, 391, 91, 23);
		contentPane.add(btnEditar);

		JSeparator separator = new JSeparator();
		separator.setBounds(10, 96, 422, 13);
		contentPane.add(separator);

		JLabel lblBusqueSeuEvento = new JLabel("Busque seu Evento");
		lblBusqueSeuEvento.setBounds(30, 32, 187, 14);
		contentPane.add(lblBusqueSeuEvento);
		
		JButton btnCancelar = new JButton("Cancelar");
		btnCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Menu menu = new Menu();
				menu.abreTela(1, funcionario);
				menu.setVisible(true);
				dispose();
			}
		});
		btnCancelar.setBounds(356, 388, 117, 29);
		contentPane.add(btnCancelar);
	}
}